package com.zhuzi.webservice;

import com.alibaba.fastjson.JSONObject;
import com.zhuzi.dto.GatherInfo;
import com.zhuzi.utils.ClientUtil;
import com.zhuzi.utils.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuzi
 * 2022/8/17 15:24
 */
@Slf4j
//@Component
/*@WebService(serviceName = "parseXml", // 与接口中指定的name一致
        targetNamespace = "http://rpb.customs.gov.cn/common/FrontEndSchemas/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.zhuzi.webservice.ParseXml"// 接口地址
)*/
public class ParseXmImpl {//implements ParseXml {
    private final static String RESP_SUCCESS = new JSONObject().fluentPut("success", "0").fluentPut("msg", "报文解析成功").toJSONString();
    String methodName = "parseFeedbackXml";
    String str82 = "<GATHER_FEEDBACK>\n" +
            "    <AREA_ID>%s</AREA_ID>\n" +
            "    <CHNL_NO>%s</CHNL_NO>\n" +
            "    <SESSION_ID></SESSION_ID>\n" +
            "    <RELLIST_TYPE>HJSasRel</RELLIST_TYPE>\n" +
            "    <RELLIST_ID_TYPE></RELLIST_ID_TYPE>\n" +
            "    <RELLIST_ID></RELLIST_ID>\n" +
            "    <FEEDBACK_TIME>2019-12-13T14:25:36.5738892+08:00</FEEDBACK_TIME>\n" +
            "    <CHECK_RESULT>%s</CHECK_RESULT>\n" +
            "    <INSTRUCTION></INSTRUCTION>\n" +
            "    <PROC_ERROR_CODE></PROC_ERROR_CODE>\n" +
            "    <PROC_ERROR_DESCRIPTION>车辆对应的核放单(状态:放行/已出库)数据, 抬杆。</PROC_ERROR_DESCRIPTION>\n" +
            "    <TECH_ERROR_CODE></TECH_ERROR_CODE>\n" +
            "    <TECH_ERROR_DESCRIPTION></TECH_ERROR_DESCRIPTION>\n" +
            "    <IC_INFO>\n" +
            "        <IC_ID></IC_ID>\n" +
            "        <IC_NO>%s</IC_NO>\n" +
            "        <IC_Type></IC_Type>\n" +
            "        <IC_EXTENDED_CONTENT></IC_EXTENDED_CONTENT>\n" +
            "    </IC_INFO>\n" +
            "    <VE_INFO>\n" +
            "        <VE_CUSTOMS_NO></VE_CUSTOMS_NO>\n" +
            "        <VE_LICENSE_NO>蒙%s</VE_LICENSE_NO>\n" +
            "        <VE_LICENSE_NO2></VE_LICENSE_NO2>\n" +
            "        <DR_CUSTOMS_NO></DR_CUSTOMS_NO>\n" +
            "        <DR_NAME></DR_NAME>\n" +
            "        <TRAILER_ID></TRAILER_ID>\n" +
            "        <WEIGHT_DIFF></WEIGHT_DIFF>\n" +
            "        <CHANNEL_WEIGHT></CHANNEL_WEIGHT>\n" +
            "        <GROSS_WEIGHT></GROSS_WEIGHT>\n" +
            "        <VEHICLE_WEIGHT></VEHICLE_WEIGHT>\n" +
            "    </VE_INFO>\n" +
            "    <CONTA_ID></CONTA_ID>\n" +
            "    <ESEAL_ID></ESEAL_ID>\n" +
            "    <SEAL_ID></SEAL_ID>\n" +
            "    <FORM_INFO>\n" +
            "        <FORM_TYPE></FORM_TYPE>\n" +
            "        <FORM_ID></FORM_ID>\n" +
            "        <MASTER_CUSTOMS_CODE></MASTER_CUSTOMS_CODE>\n" +
            "    </FORM_INFO>\n" +
            "    <PACK_NO></PACK_NO>\n" +
            "    <DECL_PACK></DECL_PACK>\n" +
            "    <DECL_GOODS_WEIGHT></DECL_GOODS_WEIGHT>\n" +
            "    <OP_HINT>车辆对应的核放单(状态:放行/已出库)数据, 抬杆。</OP_HINT>\n" +
            "    <LED_HINT>特殊监管区域放行</LED_HINT>\n" +
            "    <EXTENDED_CONTENT></EXTENDED_CONTENT>\n" +
            "</GATHER_FEEDBACK>";
    @Value("${webservice.url}")
    private String url;

    @Value("${result}")
    private String isPass;

    //@Override
    public String parseXml(String context, String type) {
        log.info("接收到信息[{}][{}]", context, type);

        CompletableFuture.runAsync(() -> {

            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException e) {

            }
            GatherInfo gatherInfo = XmlUtils.jacksonXML2Bean(context, GatherInfo.class);

            String chnlNo = gatherInfo.getChnlNo();
            String areaId = gatherInfo.getAreaId();
            String icNo = gatherInfo.getIC_INFO().getIcNo();
            System.out.println("Calling" + url);
            String veLicenseNo = gatherInfo.getVeLicenseNo();
            String result = "";
            try {
                result = ClientUtil.callWebSV(url, methodName, String.format(str82, areaId, chnlNo, isPass, icNo, veLicenseNo), "0x82");
            } catch (Exception e) {
                System.err.println("接口调用失败！！！！");
                e.printStackTrace();

            }
            System.out.println("===Finished!===恭喜你啊！！！CatService接口调用成功！！！===获得的数据是：" + result);

        });
        return RESP_SUCCESS;
    }
}
